package net.lucamasini.ricette.client.tipoingredienti;

import static net.lucamasini.ricette.client.ServiceBus.getEventbus;

import java.util.List;

import net.lucamasini.ricette.client.RicettarioRequestFactory;
import net.lucamasini.ricette.client.ServiceBus;
import net.lucamasini.ricette.client.tipoingredienti.event.NewListaTipoIngredientiEvent;
import net.lucamasini.ricette.client.tipoingredienti.event.RefreshListaTipoIngredientiEvent;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.ui.*;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SingleSelectionModel;

public class TipoIngredienti extends Composite implements NewListaTipoIngredientiEvent.Handler {
	
	private final static RicettarioRequestFactory requestFactory = GWT.create(RicettarioRequestFactory.class);

	private static TipoIngredientiUiBinder uiBinder = GWT
			.create(TipoIngredientiUiBinder.class);

	interface TipoIngredientiUiBinder extends UiBinder<Widget, TipoIngredienti> {
	}

	@UiField CellTable<TipoIngredienteProxy> tipoIngredientiCellTable;
	@UiField SimplePanel pagerContainer;

	@UiField Button crea;
	@UiField Button modifica;
	@UiField Button cancella;

	private TipoIngredientiDialog dialog;

	final SingleSelectionModel<TipoIngredienteProxy> selectionModel = new SingleSelectionModel<TipoIngredienteProxy>();

	public TipoIngredienti() {
		initWidget(uiBinder.createAndBindUi(this));

		requestFactory.initialize(ServiceBus.getEventbus());
		
	    SimplePager pager = new SimplePager();
	    pager.setDisplay(tipoIngredientiCellTable);
	    pagerContainer.setWidget(pager);
		
		tipoIngredientiCellTable.addColumn(new TextColumn<TipoIngredienteProxy>() {

			@Override
			public String getValue(TipoIngredienteProxy object) {
				return object.getDescrizione();
			}
		}, "DESCRIZIONE");
		
		tipoIngredientiCellTable.setSelectionModel(selectionModel);
		selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler(){

			@Override
			public void onSelectionChange(SelectionChangeEvent event) {
				if( selectionModel.getSelectedObject()!=null) {
					selectedButton(true);
				} else {
					selectedButton(false);
				}
			}
			
		});

		getEventbus().addHandler(NewListaTipoIngredientiEvent.TYPE, this);
		
		refreshTipoIngredienti();
		
	}

	@Override
	public void onNewListaTipoIngredientiRequest(NewListaTipoIngredientiEvent event) {

		if(dialog==null) {
			dialog = new TipoIngredientiDialog(TipoIngredienti.this);
		}
		
		List<TipoIngredienteProxy> result = event.getListaTipoIngredienti();
		
		tipoIngredientiCellTable.setRowCount(0);
		tipoIngredientiCellTable.setRowData(0, result);
		if( selectionModel.getSelectedObject()!=null) {
			selectionModel.setSelected(selectionModel.getSelectedObject(), false);
		}
	}
	
	void refreshTipoIngredienti() {
		getEventbus().fireEvent(new RefreshListaTipoIngredientiEvent(true));
	}
	
	@UiHandler("crea")
	void onCreaIngrediente(ClickEvent event){
		dialog.creaIngrediente();
	}
	
	@UiHandler("modifica")
	void onModificaIngrediente(ClickEvent event){
		final TipoIngredienteProxy ingrediente = selectionModel.getSelectedObject();

		dialog.modificaTipoIngrediente(ingrediente);		
	}
	
	@UiHandler("cancella")
	void onCancellaIngrediente(ClickEvent event){
		final TipoIngredienteProxy ti = selectionModel.getSelectedObject();

		requestFactory.tipoIngredienteRequest().deleteTipoIngrediente(ti).fire();
	}
	
	@UiHandler("ricarica")
	void onRicaricaIngrediente(ClickEvent event){
		refreshTipoIngredienti();
	}

	private void selectedButton(boolean selected) {
		modifica.setEnabled(selected);
		cancella.setEnabled(selected);
	}
}
